package com.newlink.building.userinfo.utils

import android.util.Log
import com.newlink.building.common_base.ext.toPaddedHexString
import com.newlink.building.common_base.fastble.utils.Base_HexUtil
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.util.Collections
import java.util.Date

/**
 * @Author: Jake
 * @Date: 2024-05-17
 * @Description:
 */
private const val TAG: String = "write_data_jake"
const val Action_Open: String = "01"

fun doCryptWork(content: String, key: String): String {
    Log.e(TAG, "<doCryptWork>. 原始数据 :: $content")
    val encryptContent1 = Module_UserInfo_AESUtils.encrypt(content, key)
    Log.e(TAG, "<doCryptWork> aes 加密结果 :: $encryptContent1")
    val actionBuilder = StringBuilder()
    val testMode = true
    val lengthValue = encryptContent1.length
    // aes 结果 + Crc 长度
    // java 字符串长度 转成 C 语言的 16 进制长度
    val realLength = ((lengthValue + 4) / 2).toString(16)
    if (testMode) {

        val lengthValue16 = (encryptContent1.length + 4).toString(16)
        Log.e(TAG, "<doCryptWork> lengthValue = $lengthValue , lengthValue16 = $lengthValue16")
        actionBuilder.append(Action_Open)
        actionBuilder.append(realLength)
        actionBuilder.append(encryptContent1)
    } else {
        actionBuilder.append(Action_Open)
        actionBuilder.append("01")
        actionBuilder.append(encryptContent1)
    }

    val withoutCRC = actionBuilder.toString()
    Log.e(TAG, "<doCryptWork> AES加密后 前面拼接01 withoutCRC:: $withoutCRC")
//    val crc = CRC16CCITT.calculateCRC16_CCITT(CRC16CCITT.hexStringToByteArray(withoutCRC))
//        .toString(16)
//    val crc2 = CRC16CCITT.calculateCRC16_CCITT2(CRC16CCITT.hexStringToByteArray(withoutCRC))
//        .toString(16)
//    Log.e(TAG, "<doCryptWork> AES加密后 前面拼接01 在用CRC16CCITT 加密 得到 crc:: $crc")
//    Log.e(TAG, "<doCryptWork> AES加密后 前面拼接01 在用CRC16CCITT 加密 得到 crc2:: $crc2")

    val crc3 = Module_UserInfo_CRC16CCITT.calculateCRC16_CCITT2(
        Module_UserInfo_CRC16CCITT.hexStringToByteArray(withoutCRC))
        .toPaddedHexString()
    Log.e(
        TAG,
        "<doCryptWork> AES加密后 TYPE: 01 | LENGTH: ${(lengthValue + 4) / 2} | VALUE : ${withoutCRC} + crc3:: $crc3 | crc 长度 ${crc3.length}"
    )
    val finalEncryptVal = "$withoutCRC$crc3"
    Log.e(TAG, "<doCryptWork> 最终拼接结果 withoutCRC + crc:: $finalEncryptVal")
    return finalEncryptVal
}

fun longToByteArray(value: Long): String {
//        val value = 1775354049567285249L
    val bb = ByteBuffer.allocate(java.lang.Long.BYTES)
    bb.order(ByteOrder.BIG_ENDIAN)
    bb.putLong(value)
    val bytes = bb.array()
    for (b in bytes) {
        System.out.printf("jake ... %02X ", b)
    }
    val k = Base_HexUtil.formatHexString(bytes)
    Log.e(TAG, "<longToByteArray> result : $k")
    return k
}

fun padWithZeros(number: String, totalLength: Int): String? {
    val padding = totalLength - number.length
    return if (padding > 0) {
        number + java.lang.String.join("", Collections.nCopies(padding, "0"))
    } else {
        number
    }
}

fun getSecondTimestampTwo(date: Date): String {
    return java.lang.String.valueOf(date.time / 1000)
}